﻿using DAL.comm;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public  class ExaminPurchaseBillDAL
    {
        private SqlCommand cmd;
        public ExaminPurchaseBillDAL()
        {

            cmd = new SqlCommand();//实例化sqlcommand对象
            cmd.CommandType = CommandType.StoredProcedure;//定义sqlmmand对象访问数据库方式为存储过程
        }
        public bool ForEdit(SqlCommand cmd)
        {
            try
            {
                int num = SQLHelper.ExecuteSQL(cmd);
                if (num > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public bool PassExamine(PurchaseBill bo)
        {
            //SqlTransaction transaction = SQLHelper.ExcuteSqlTransaction(cmd);
            //try
            //{
                cmd.CommandText = "UpdateExaminerByPurchaseID";//存储过程名
                cmd.Parameters.Clear();//清空存储过程参数
                cmd.Parameters.Add("@PurchaseID", SqlDbType.NChar, 12).Value = bo.PurchaseBillID ;//添加存储过程参数
                cmd.Parameters.Add("@Examiner", SqlDbType.NVarChar, 6).Value = bo.Examiner ;//添加存储过程参数
                cmd.Parameters.Add("@Memo", SqlDbType.NVarChar, 100).Value = bo.Memo ;//添加存储过程参数

                //    int num = cmd.ExecuteNonQuery ();
                //    if (num > 0)
                //    {
                //        transaction.Commit();
                //        return true;
                //    }
                //    else
                //        return false;

                //}
                //catch
                //{
                //    transaction.Rollback();//回滚
                //    return false;
                //}
                //finally
                //{
                //    SQLHelper.CloseConn();
                //}
                return ForEdit(cmd);
        }
        /// <summary>
        ///获取待审核订单列表 
        /// </summary>
        /// <returns></returns>
    public List <string > GetPendingPurchaseBill()
        {
            cmd.CommandText = "GetPendingPurchaseBill";//存储过程名
            cmd.Parameters.Clear();//清空存储过程参数
            List<string> list = new List<string>();
            SqlDataReader reader = SQLHelper.ExcuteReader(cmd);//读库 
            while (reader .Read())
            {
                string id = reader["PurchaseID"].ToString();
                list.Add(id);
            }
            reader.Close();//关闭读库
            return list;
        }
        /// <summary>
        ///审核不通过 
        /// </summary>
        /// <param name="bo"></param>
        /// <returns></returns>
        public bool NoPassExamine(PurchaseBill bo)
        {
            SqlTransaction transaction = SQLHelper.ExcuteSqlTransaction(cmd);
            try
            {
                cmd.CommandText = "NotPassExaminerByPurchaseID";//存储过程名
                cmd.Parameters.Clear();//清空存储过程参数
                cmd.Parameters.Add("@PurchaseID", SqlDbType.NChar, 12).Value = bo.PurchaseBillID;//添加存储过程参数
                cmd.Parameters.Add("@Examiner", SqlDbType.NVarChar, 6).Value = bo.Examiner;//添加存储过程参数
                cmd.Parameters.Add("@Memo", SqlDbType.NVarChar, 100).Value = bo.Memo;//添加存储过程参数

                int num = cmd.ExecuteNonQuery();
                if (num > 0)
                {
                    transaction.Commit();
                    return true;
                }
                else
                    return false;

            }
            catch
            {
                transaction.Rollback();//回滚
                return false;
            }
            finally
            {
                SQLHelper.CloseConn();
            }
        }
    }
}
